package com.zabra.zl.ui.deposit.presenter;

import com.zabra.zl.App;
import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.DepositParmasBean;
import com.zabra.zl.ui.deposit.contract.DepositContract;
import com.zabra.zl.ui.deposit.model.DepositModel;
import com.zabra.zl.utils.SharedPreferencesUtil;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DepositPresenter extends BasePresenter<DepositContract.View> implements DepositContract.Presenter {
    private DepositContract.Model model;

    public DepositPresenter() {
        model = new DepositModel();
    }

    @Override
    public void getData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(body).enqueue(new Callback<DepositParmasBean>() {
            @Override
            public void onResponse(Call<DepositParmasBean> call, Response<DepositParmasBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<DepositParmasBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }
}
